package com.airS.task.fileMgr.service.sys.impl;

import com.airS.task.fileMgr.common.page.Page;
import com.airS.task.fileMgr.example.sys.SysOperalogExample;
import com.airS.task.fileMgr.mapper.sys.SysOperalogMapper;
import com.airS.task.fileMgr.model.sys.SysOperalog;
import com.airS.task.fileMgr.query.sys.SysOperalogQuery;
import com.airS.task.fileMgr.service.sys.SysOperalogService;
import com.airS.task.fileMgr.utils.page.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by alan on 2017/3/17.
 */
@Service
public class SysOperalogServiceImpl implements SysOperalogService {

    @Autowired
    private SysOperalogMapper sysOperalogMapper;

    @Override
    public Page<SysOperalog> queryYunfiles(SysOperalogQuery query) {
        PageUtil.startPage(query);
        List<SysOperalog> sysOperalogs = sysOperalogMapper.selectByExample(assemblySysOperalogExample(query));
        return new Page<SysOperalog>(sysOperalogs);
    }

    private SysOperalogExample assemblySysOperalogExample(SysOperalogQuery query){
        SysOperalogExample example = new SysOperalogExample();
        SysOperalogExample.Criteria criteria = example.createCriteria();
        if(StringUtils.hasText(query.getEmployeeName())){
            criteria.andEmployeeNameLike("%" + query.getEmployeeName() + "%");
        }
        if(StringUtils.hasText(query.getClientIp())){
            criteria.andClientIpLike("%" + query.getClientIp() + "%");
        }
        if(StringUtils.hasText(query.getServerIp())){
            criteria.andServerIpLike("%" + query.getServerIp() + "%");
        }
        if(StringUtils.hasText(query.getLogType())){
            criteria.andLogTypeEqualTo(query.getLogType());
        }
        if(StringUtils.hasText(query.getOperaStatus())){
            criteria.andOperaStatusEqualTo( query.getOperaStatus());
        }
        // 默认排序，按时间倒序
        example.setOrderByClause("create_time desc");
        return example;
    }

}
